// Environment.cpp
#include "Environment.h"
#include <QPainter>
#include <QRandomGenerator>

Environment::Environment(QObject* parent) 
    : QGraphicsScene(parent) {
    drawGround();
}

void Environment::drawGround() {
    // 绘制灰色地面
    addRect(-1000, -1000, 2000, 2000, Qt::NoPen, QColor(200, 200, 200));
    
    generateCircularPath();
}

void Environment::generateRandomPath() {
    trackPath = QPainterPath();
    trackPath.moveTo(-800, 0);
    
    // 使用随机控制点生成贝塞尔曲线
    qreal ctrl1X = QRandomGenerator::global()->bounded(-500, 500);
    qreal ctrl1Y = QRandomGenerator::global()->bounded(-500, 500);
    qreal ctrl2X = QRandomGenerator::global()->bounded(-500, 500);
    qreal ctrl2Y = QRandomGenerator::global()->bounded(-500, 500);
    
    trackPath.cubicTo(ctrl1X, ctrl1Y, ctrl2X, ctrl2Y, 800, 0);
    addPath(trackPath, QPen(Qt::black, 20));
}
void Environment::generateCircularPath() {
    trackPath = QPainterPath();

    // 圆心位置和半径
    QPointF center(0, 0);
    qreal radius = 500;
    
    // 绘制圆形路径
    trackPath.moveTo(center.x() + radius, center.y());  // 起始点在圆的右端
    for (int i = 1; i <= 100; ++i) {
        qreal angle = 2 * M_PI * i / 100;  // 分100个点均匀分布
        qreal x = center.x() + radius * cos(angle);  // 计算 x 坐标
        qreal y = center.y() + radius * sin(angle);  // 计算 y 坐标
        trackPath.lineTo(x, y);  // 添加每个点到路径
    }
    
    trackPath.closeSubpath();  // 闭合路径，形成闭环
    addPath(trackPath, QPen(Qt::black, 20));  // 使用黑色线条绘制路径
}